

import UIKit

import UIKit

private let reuseIdentifier = "videocell"

class videoTableViewController: UITableViewController
{
    
    
    var videolist:[AnyObject]!
    var pagenumber:Int = 1
    @IBAction func loadnextpage(_ sender: UIButton)
    {
        self.pagenumber+=1
        get_list_info()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
        get_list_info()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        if self.videolist != nil{
            return videolist.count
        }
        return 1
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let videoStoryboard = UIStoryboard(name: "video", bundle: nil)
        let vd = videoStoryboard.instantiateViewController(withIdentifier: "playvideo") as! playvideoViewController
        //取模型
        vd.videoModle = self.videolist[indexPath.row] as? resourcejson
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vd, animated: true)
        self.hidesBottomBarWhenPushed = false
    }
    

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! videoTableViewCell
        // Configure the cell
        if self.videolist != nil{
            let model = self.videolist[indexPath.row] as! resourcejson
            
            cell.videotitle.text = model.name!
            cell.videoauthor.text = ("作者: "+model.author!)
            cell.videotime.text = model.update_time
            if model.thumb != nil{
                let imgUrlPre = "http://amicool.neusoft.edu.cn/Uploads/"
                let imgUrl = URL(string: imgUrlPre + model.thumb!)
                let imgData = try! Data(contentsOf: imgUrl!)
                cell.videoimg.image = UIImage(data: imgData)?.scaleImageToSize(CGSize(width: 80, height: 60))
            }
            else{
                cell.videoimg.image = UIImage(named: "xiaohui.jpg")
            }
        }
        
        return cell
        
    }
    
    func get_list_info()
    {
        let parameters:NSDictionary = ["SessionID":networktool.loginReturnModel!.sessionid!,
                                       "mod":"video",
                                       "page":self.pagenumber
        ]
        networktool.shared.get_list(parameters){ (list) in
            DispatchQueue.main.async {
                if list != nil{
                    if(self.pagenumber == 1)
                    {
                        self.videolist = [AnyObject]()
                    }
                    let check:Int = self.videolist.count
                    for course in list!{
                        let model = resourcejson.returnjson("video",course as? NSDictionary)
                        if( model?.name?.characters.count != 0 )
                        {
                            self.videolist.append(model!)
                        }
                    }
                    if(check == self.videolist.count)
                    {
                        pompalert(uvc: self, message: "已经是最后一页了")
                    }else
                    {
                        self.tableView.reloadData()
                    }
                }
                else{
                    pompalert(uvc: self, message: "加载页面出错" )
                }
            }
            
        }
    }
    
    
}
